package com.dmap.admin.controller.background;

import com.dmap.admin.controller.background.param.PermissionParam;
import com.dmap.admin.po.AdminPermissions;
import com.dmap.admin.service.backgroundv1.PermissionService;
import com.dmap.admin.vo.PermissionVo;
import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * \* User: silent
 * \* Date: 08/12/17 Time: 11:28
 * \* Description:
 * \
 */
@Api(value = "权限管理", description = "background/权限管理")
@Controller
@RequestMapping(value = "/background/pmpd-admin-server/v1/permission/")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    @ResponseBody
    @ApiOperation(value = "add 权限",
            notes = "1.必填：name, url, 返回值为：id;" +
                    "2.hasChild标记是否有子菜单项 0-无子菜单，1-有子菜单")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public BaseVo insert(@RequestBody PermissionParam param) {
        BaseVo baseVo = new BaseVo();
        if (param == null || StringUtils.isBlank(param.getName()) || StringUtils.isBlank(param.getUrl())) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        AdminPermissions adminPermissions = permissionService.insert(param);
        if (adminPermissions == null) {
            baseVo.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
            return baseVo;
        }
        baseVo.setData(adminPermissions.getId().toString());
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "update 权限", notes = "必填：id, name, url")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public BaseVo update(@RequestBody PermissionParam param) {
        BaseVo baseVo = new BaseVo();
        if (param == null || StringUtils.isBlank(param.getId())
                || StringUtils.isBlank(param.getName()) || StringUtils.isBlank(param.getUrl())) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        AdminPermissions adminPermissions = permissionService.update(param);
        if (adminPermissions == null) {
            baseVo.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
            return baseVo;
        }
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "delete 权限 by id", notes = "require id")
    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    public BaseVo delete(@RequestParam(name = "id") String id) {
        BaseVo baseVo = new BaseVo();
        if (StringUtils.isBlank(id)) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        if (permissionService.delete(Long.valueOf(id))) {
            return baseVo;
        }
        baseVo.setCode(Constant.RESPONSE_DELETE_ERROR_CODE);
        baseVo.setMsg(Constant.RESPONSE_DELETE_ERROR_MSG);
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "select all 权限", notes = "select all 权限")
    @RequestMapping(value = "select/selectAllPermission", method = RequestMethod.GET)
    public BaseVo selectAllPermission() {
        BaseVo baseVo = new BaseVo();
        List<AdminPermissions> adminPermissionsList = permissionService.selectAllPermission();
        List<PermissionVo> permissionVoList = new ArrayList<>();
        adminPermissionsList.forEach(adminPermissions -> permissionVoList.add(new PermissionVo(adminPermissions)));
        baseVo.setData(permissionVoList);
        return baseVo;
    }

}
